--[[
@module Main
]]

LIB_PATH = "lib."

local fennel = require(LIB_PATH.."fennel.fennel")
table.insert(package.loaders or package.searchers, fennel.searcher)
pl = require (LIB_PATH.."pl.import_into")()

_lg = love.graphics
_li = love.image
_lk = love.keyboard
_lm = love.mouse
_lf = love.filesystem
_lw = love.window
_lt = love.timer

local Effects

function love.load(file)
	_lg.setDefaultFilter("nearest", "nearest")
	_lf.setIdentity("sled")
	_lk.setKeyRepeat (true)
	_lw.maximize()
    _lg.setLineWidth(.1)

	DataPath = _lf.getAppdataDirectory().."love/sled/"
	local TileSize = 8
	Sled = require "sled"(TileSize, 512, 512)
	local Moonshine = require (LIB_PATH.."moonshine")
   if not _lf.getInfo("maps") then
      _lf.createDirectory("maps")
   end
   if not _lf.getInfo("tiles") then
      _lf.createDirectory("tiles")
   end
   if not _lf.getInfo("layers") then
      _lf.createDirectory("layers")
   end
   
   Effects = Moonshine.chain(Moonshine.effects.crt).chain(Moonshine.effects.chromasep)
   Effects.parameters = {
      crt = {
	 distortionFactor = {1, 1}
      },
      chromasep = {
	 angle = 1.1,
	 radius = .8
      }
   }
end

function love.update(dt)
   Sled:update(dt)
end

function love.draw()
   Effects.draw(function ()
	 Sled:draw()
   end)
   
end

function love.keypressed(key)
   Sled.input:press_key (key)
end

function love.resize(w, h)
	if Sled.current_screen then
   Sled.current_screen:resize(w, h)
	end
   Effects.resize(w, h)
end

function love.mousepressed(x, y, button)
   Sled.input:press_mouse(button)
end

function love.wheelmoved(x, y)
   if Sled.state == "focused" then
      Sled.gui.palette.offsetY = Sled.gui.palette.offsetY + math.floor(y*24)
   elseif Sled.state == "map" then
      Sled.current_screen:zoom_on_mouse_by (y/50)
   end
end

function love.quit()
	if Sled.state == "splash" then return end
   Sled.gui.command_bar.history:save()
end

function love.textinput(text)
   Sled.input:ti_press_key (text)
end
